import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';

import '../../public_gyms_page/models/public_gym.dart';
/*
class Gym {
  final String gymId;
  final String name;
  final String description;
  String? image;
  File? imageFile;
  final String address;
  final String workingHours;
  bool? active;
  String? isDeleted;
  final DateTime? createAt;
  final DateTime? updateAt;
  final String? createdBy;

  Gym({
    this.gymId = '-1',
    required this.name,
    required this.description,
    this.image,
    this.imageFile,
    required this.address,
    required this.workingHours,
    this.active,
    this.isDeleted,
    this.createAt,
    this.updateAt,
    this.createdBy,
  }){
    if(image != null && image!.isEmpty){
      image = null;
    }

  }

  factory Gym.fromRawJson(String str) => Gym.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory Gym.fromJson(Map<String, dynamic> json) => Gym(
    gymId: json["gym_id"].toString(),
    name: json["name"],
    description: json["description"],
    image: json["image"],
    address: json["address"],
    workingHours: json["working_hours"],
    active: json["active"]?.toString() == '1',
    isDeleted: json["is_deleted"]?.toString(),
    createAt: DateTime.parse(json["create_at"]),
    updateAt: DateTime.parse(json["update_at"]),
    createdBy: json["created_by"]?.toString(),
  );
  factory Gym.fromPublicGym(PublicGym gym) => Gym(
    gymId: gym.gymId??'-1',
    name: gym.name,
    description: gym.description,
    image: gym.image,
    address: gym.address,
    workingHours: gym.workingHours,
    active: gym.active=='1',
    isDeleted: gym.isDeleted,
    createAt: gym.createAt,
    updateAt: gym.updateAt,
    createdBy: gym.createdBy,
  );
  Gym copyWith({
    String? gymId,
    String? name,
    String? description,
    String? image,
    File? imageFile,
    String? address,
    String? workingHours,
    bool? active,
    String? isDeleted,
    DateTime? createAt,
    DateTime? updateAt,
    String? createdBy,
  }) => Gym(
    gymId:gymId??this.gymId,
    name: name??this.name,
    description: description??this.description,
    image: image??this.image,
    address: address??this.address,
    workingHours: workingHours??this.workingHours,
    active: active??this.active,
    isDeleted: isDeleted??this.isDeleted,
    createAt: createAt??this.createAt,
    updateAt: updateAt??this.updateAt,
    createdBy: createdBy??this.createdBy,
  );
  Future<Map<String, dynamic>> toJson()async => {
    "gym_id": gymId,
    "name": name,
    "description": description,
    if(imageFile != null)
      'image':await MultipartFile.fromFile( imageFile!.path, filename: imageFile!.path.split('/').last ),
    "address": address,
    "working_hours": workingHours,
    "active": (active == true)?'1':'0',
  };
}
*/


class Gym {
  final String gymId;
  final String name;
  final String description;
  String? image;
  File? imageFile;
  final String address;
  final String workingHours;
  int? active; // تغيير إلى int
  int? isDeleted; // تغيير إلى int
  final DateTime? createAt;
  final DateTime? updateAt;
  final String? createdBy;
  // خصائص جديدة
  int? adminsSubscriptionsId;
  String? startTime;
  String? endTime;
  num? latitude;
  num? longitude;
  num? recentAttendanceCount;

  Gym({
    this.gymId = '-1',
    required this.name,
    required this.description,
    this.image,
    this.imageFile,
    required this.address,
    required this.workingHours,
    this.active,
    this.isDeleted,
    this.createAt,
    this.updateAt,
    this.createdBy,
    this.adminsSubscriptionsId,
    this.startTime,
    this.endTime,
    this.latitude,
    this.longitude,
    this.recentAttendanceCount,
  }) {
    if (image != null && image!.isEmpty) {
      image = null;
    }
  }

  factory Gym.fromRawJson(String str) => Gym.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory Gym.fromJson(Map<String, dynamic> json) => Gym(
    gymId: json["gym_id"].toString(),
    name: json["name"],
    description: json["description"],
    image: json["image"],
    address: json["address"],
    workingHours: json["working_hours"],
    active:int.tryParse(json["active"].toString())  ,
    isDeleted: int.tryParse(json["is_deleted"].toString()),
    createAt: json["create_at"] != null ? DateTime.parse(json["create_at"]) : null,
    updateAt: json["update_at"] != null ? DateTime.parse(json["update_at"]) : null,
    createdBy: json["created_by"]?.toString(),
    adminsSubscriptionsId: int.tryParse(json["admins_subscriptions_id"].toString()),
    startTime: json["start_time"],
    endTime: json["end_time"],
    latitude: num.tryParse(json["latitude"].toString()),
    longitude: num.tryParse(json["longitude"].toString()),
    recentAttendanceCount: num.tryParse(json["recent_attendance_count"].toString()),
  );

  factory Gym.fromPublicGym(PublicGym gym) => Gym(
    gymId: gym.gymId ?? '-1',
    name: gym.name,
    description: gym.description,
    image: gym.image,
    address: gym.address,
    workingHours: gym.workingHours,
    active: gym.active == 1 ? 1 : 0,
    isDeleted: gym.isDeleted == 1 ? 1 : 0,
    createAt: gym.createAt,
    updateAt: gym.updateAt,
    createdBy: gym.createdBy,
    adminsSubscriptionsId: gym.adminsSubscriptionsId,
    startTime: gym.startTime,
    endTime: gym.endTime,
    latitude: gym.latitude,
    longitude: gym.longitude,
    recentAttendanceCount: gym.recentAttendanceCount,
  );

  Gym copyWith({
    String? gymId,
    String? name,
    String? description,
    String? image,
    File? imageFile,
    String? address,
    String? workingHours,
    int? active,
    int? isDeleted,
    DateTime? createAt,
    DateTime? updateAt,
    String? createdBy,
    int? adminsSubscriptionsId,
    String? startTime,
    String? endTime,
    num? latitude,
    num? longitude,
    num? recentAttendanceCount,
  }) => Gym(
    gymId: gymId ?? this.gymId,
    name: name ?? this.name,
    description: description ?? this.description,
    image: image ?? this.image,
    address: address ?? this.address,
    workingHours: workingHours ?? this.workingHours,
    active: active ?? this.active,
    isDeleted: isDeleted ?? this.isDeleted,
    createAt: createAt ?? this.createAt,
    updateAt: updateAt ?? this.updateAt,
    createdBy: createdBy ?? this.createdBy,
    adminsSubscriptionsId: adminsSubscriptionsId ?? this.adminsSubscriptionsId,
    startTime: startTime ?? this.startTime,
    endTime: endTime ?? this.endTime,
    latitude: latitude ?? this.latitude,
    longitude: longitude ?? this.longitude,
    recentAttendanceCount: recentAttendanceCount ?? this.recentAttendanceCount,
  );

  Future<Map<String, dynamic>> toJson() async => {
    "gym_id": gymId,
    "name": name,
    "description": description,
    if (imageFile != null)
      'image': await MultipartFile.fromFile(imageFile!.path, filename: imageFile!.path.split('/').last),
    "address": address,
    "working_hours": workingHours,
    "active": (active == 1)?'1':'0',
    "is_deleted": isDeleted ?? 0,
    "create_at": createAt?.toIso8601String(),
    "update_at": updateAt?.toIso8601String(),
    "created_by": createdBy,
    "admins_subscriptions_id": adminsSubscriptionsId,
    "start_time": startTime,
    "end_time": endTime,
    "latitude": latitude,
    "longitude": longitude,
    "recent_attendance_count": recentAttendanceCount,
  };
}
